let api = require('../../../api/api.js')
const app = getApp()
Page({
  /**
   * 页面的初
   */
  data: {
    showLink: true,
    buyBox: [],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    imgNum: 0,
    num: 1,
    tokenState: false,
    id: '',
    mark: false,
    shoplistid: 0,
    hiddenx: true,
    setInter: '',
    hideDonghua: true,
    animationSrc: "/pages/img/6.png",
    animationHide: true,
    hide_good_box: true
  },

  swiperChange: function(e) {
    var that = this;
    that.setData({
      imgNum: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var s = 10

    wx.getSystemInfo({
      success: function(res) {
        if (res.model.indexOf("iPhone X") > -1) {
          s = 40
          that.setData({
            hiddenx: false
          })
        }
        if (res.model.indexOf("unknown<iPhone11,2>") > -1) {
          s = 40
          that.setData({
            hiddenx: false
          })
        }
        if (res.model.indexOf("unknown<iPhone11,8>") > -1) {
          s = 40
          that.setData({
            hiddenx: false
          })
        }
        if (res.model.indexOf("unknown<iPhone11,4>") > -1) {
          s = 40
          that.setData({
            hiddenx: false
          })
        }

        if (res.model.indexOf("iPhone") > -1) {
          that.setData({
            ios: true
          })
        }
      }
    })
    this.busPos = {};
    this.busPos['x'] = app.globalData.ww - app.globalData.ww * 0.68;
    this.busPos['y'] = app.globalData.hh - s;
    console.log(this.busPos['x'])
    console.log(this.busPos)

    if (options.show == "false") {
      this.setData({
        "showLink": false
      })
    }

    let id = options.id
    this.setData({
      id: this.options.id,
      type: options.type
    })

    var data = options;
    let token = wx.getStorageSync('token')
    if (token != undefined) {
      data = {
        id: options.id,
        s_id: options.s_id,
        type: options.type,
        token: wx.getStorageSync('token')
      }
    } else {
      data = {
        id: options.id,
        s_id: options.s_id,
        type: options.type,
      }
    }
    if (options.type == 1) {
      api.newBuyContent(data, (res) => {
        console.log(res)
        this.setData({
          buyContent: res.data.result,
          options: options
        })
      })
    } else {
      api.buyContent(data, (res) => {
        console.log(res)
        this.setData({
          buyContent: res.data.result,
          animationSrc: res.data.result.file_url[0].file_url,
          options: options
        })
      })
    }

    setTimeout(function() {
      that.setData({
        hideDonghua: false
      })
    }, 1700)

    console.log(data, '调用的接口是获取商品详情的')
  },

  touch: function(e) {
    console.log(e)
  },

  urlState: function(e) {
    console.log(e)
    let state = e.currentTarget.dataset.state
    if (state == 1) {
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
    if (state == 2) {
      wx.makePhoneCall({
        phoneNumber: '18511175656',
      })
    }
    if (state == 3) {
      wx.switchTab({
        url: '/pages/buy/index/index'
      })
    }
  },
  zf: function() {
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.animation = wx.createAnimation()
  },
  endSetInter: function() {
    var that = this;
    //清除计时器  即清除setInter
    clearInterval(that.data.setInter)
    this.animation = wx.createAnimation()
    this.animation.scale(Math.random() / 2)
    this.animation.translateY(1300).step({
      duration: 3000
    })
    this.setData({
      animation: this.animation.export()
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.getSetting({ //   判断是否授权
      success: (res) => {}
    })
  },

  dow: function() {
    this.setData({
      mark: false
    })
  },
  /*分类选择*/
  select: function(e) {
    let id = `.buyContent.id`
    this.setData({
      [id]: e.currentTarget.id,
      shoplistid: e.currentTarget.id
    })
    let data = {
      c_id: e.currentTarget.dataset.cate_id,
      id: e.currentTarget.id
    }
    this.buyBox(data)
    var param;
    let token = wx.getStorageSync('token')
    // console.log(token)
    if (token != undefined) {
      param = {
        id: e.currentTarget.dataset.cate_id,
        s_id: e.currentTarget.id,
        type: 1,
        token: wx.getStorageSync('token')
      }
    } else {
      param = {
        id: e.currentTarget.dataset.cate_id,
        s_id: e.currentTarget.id,
        type: 1,
      }
    }
    console.log(param)
    api.buyContent(param, (res) => {
      console.log(res)
      this.setData({
        buyContent: res.data.result,
        animationSrc: res.data.result.file_url[0].file_url,
      })
    })
  },

  capacitySelect: function(e) {
    let id = `.buyContent.id`
    this.setData({
      [id]: e.currentTarget.id,
      shoplistid: e.currentTarget.id
    })
    let data = {
      c_id: e.currentTarget.dataset.cate_id,
      id: e.currentTarget.id
    }
    this.buyBox(data)
    var param;
    let token = wx.getStorageSync('token')
    if (token != undefined) {
      param = {
        id: e.currentTarget.dataset.cate_id,
        s_id: e.currentTarget.id,
        type: 1,
        token: wx.getStorageSync('token')
      }
    } else {
      param = {
        id: e.currentTarget.dataset.cate_id,
        s_id: e.currentTarget.id,
        type: 1,
      }
    }
    console.log(param)
    api.newBuyContent(param, (res) => {
      console.log(res)
      this.setData({
        buyContent: res.data.result,

      })
    })
  },

  /* 加*/
  add: function() {
    let num = this.data.num
    if (num <= 99) {
      this.setData({
        num: this.data.num + 1
      })
    }
  },
  /* 减 */
  remove: function() {
    let num = this.data.num
    if (num > 1) {
      this.setData({
        num: this.data.num - 1
      })
    }
  },
  /* 确定选择*/
  confirm: function(e) {
    var that = this
    console.log(this.data.shoplistid)
    var data = {}
    if (this.data.shoplistid == 0) {
      data.id = this.data.buyContent.id
      data.num = this.data.num
    } else {
      data.id = this.data.shoplistid
      data.num = this.data.num
    }
    let token = wx.getStorageSync("token")
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.setData({
      mark: false
    })
    console.log(data)
    if (this.data.seletId == 1) {
      console.log(this.data.seletId)
      this.buyListS(data)
    }
    if (this.data.seletId == 2) {
      console.log(this.data.buyContent.id)
      let data = []
      data.push({
        num: this.data.num,
        sc_id: this.data.buyContent.id,
        type: 1
      })
      let datas = JSON.stringify(data)
      wx.setStorageSync('datas', datas)
      console.log(data)
      wx.navigateTo({
        url: `/pages/buy/create/create`,
      })
    }
  },
  touch: function(e) {
    console.log(e)
  },
  buyListS(data) {
    var that = this
    api.buyList(data, (res) => {
      console.log(res)
      var options = {};
      options = this.data.options
      options.id = this.data.buyContent.cate_id
      options.s_id = this.data.buyContent.id
      options.show = false
      options.type = 1
      this.onLoad(options)
      this.finger = {};
      var topPoint = {};
      this.finger['x'] = 300;
      this.finger['y'] = 208;
      if (this.finger['y'] < this.busPos['y']) {
        topPoint['y'] = this.finger['y'] - 150;
      } else {
        topPoint['y'] = this.busPos['y'] - 150;
      }
      if (this.finger['x'] > this.busPos['x']) {
        topPoint['x'] = (this.finger['x'] - this.busPos['x']) / 2 + this.busPos['x'];
      } else {
        topPoint['x'] = (this.busPos['x'] - this.finger['x']) / 2 + this.finger['x'];
      }
      this.linePos = app.bezier([this.busPos, topPoint, this.finger], 100);
      this.startAnimation();

    })
  },
  startAnimation: function(e) {
    var index = 0,
      that = this,
      bezier_points = that.linePos['bezier_points'];

    this.setData({
      hide_good_box: false,
      bus_x: that.finger['x'],
      bus_y: that.finger['y']
    })
    var len = bezier_points.length;
    index = len
    this.timer = setInterval(function() {
      for (let i = index - 1; i > -1; i--) {
        that.setData({
          bus_x: bezier_points[i]['x'],
          bus_y: bezier_points[i]['y']
        })

        if (i < 1) {
          clearInterval(that.timer);
          // that.addGoodToCartFn(e);
          that.setData({
            hide_good_box: true
          })
        }
      }
    }, 25);
  },
  /* 底部 加入购买*/
  Cart: function(e) {
    this.setData({
      seletId: e.currentTarget.id
    })

    let data = {
      c_id: this.data.buyContent.cate_id,
      id: this.data.buyContent.id
    }

    this.buyBox(data)

    let token = wx.getStorageSync("token")
    if (token) {
      this.setData({
        mark: true,
        tokenState: false
      })
    } else {
      this.setData({
        tokenState: true
      })
    }
  },

  buyBox(data) {
    console.log(data, '传过去的商品值')
    api.gouwuche(data, (res) => {
      console.log(res)
      var result = res.data.result
      var buyBox = {}
      result.forEach(function(element) {
        if (element.choice == 1) {
          buyBox = element
        }
      })
      console.log(buyBox)
      this.setData({
        buyBox: buyBox
      })
    })
  },
  data4() {
    wx.navigateTo({
      url: `/pages/buy/buyConten/buyConten?id=${0}&show=false`,
    })
  },
  buy: function() {
    let id = this.data.id
    let nums = this.data.num
    console.log(id)
    if (id == '') {
      this.setData({
        mark: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var that = this;
    //清除计时器  即清除setInter
    clearInterval(that.data.setInter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }, //授权
  onGotUserInfo: function(e) {
    var S = this;
    wx.getUserInfo({
      success: function(res) {
        let userInfo = e.detail.userInfo
        let data = {
          user_name: e.detail.userInfo.nickName,
          user_img: e.detail.userInfo.avatarUrl,
          openid_wechat: wx.getStorageSync("key").openid,
          level: app.globalData.level
        }

        api.tokens(data, (res) => {
          console.log(res)
          wx.setStorageSync('token', res.data.result)
          S.setData({
            tokenState: false
          })
        })
      },
      fail: function(err) {
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
    console.log(e)


  },
})